package org.aeon.client.view.components.sessionPanel;

import lombok.Getter;
import org.aeon.client.context.GlobalContext;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * @author: MissingAeon
 * @date: 2024/2/20 13:00
 * @description:
 */

@Getter
public class SessionPanel extends JPanel {
    private final DefaultPanel defaultPanel = new DefaultPanel();

    public SessionPanel() {
        GlobalContext.getContext().setSessionPanel(this);
        this.setLayout(new GridLayout(1, 1));
        this.setPreferredSize(new Dimension(700, 700));
        Border border = BorderFactory.createMatteBorder(1, 1, 1, 1,
                new Color(0xA0595959, true));
        this.setBorder(border);
    }

    /**
     * 替换
     *
     * @param comp the component to be added
     * @return
     */
    @Override
    public Component add(Component comp) {
        SwingUtilities.invokeLater(() -> {
            this.removeAll();
            super.add(comp);
            this.revalidate();
            this.repaint();
        });
        return comp;
    }

    /**
     * 获取该页面正在展示的组件
     * 如果不是需要的类型则返回空
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getPanel(Class<T> clazz) {
        final Component[] components = this.getComponents();
        if (components.length > 0) {
            final Component component = components[0];
            if (clazz.isInstance(component)) {
                return clazz.cast(component);
            }
        }
        return null;
    }
}
